﻿app.factory("Albums", function () { return []; });
app.factory("Songs", function () { return []; });

app.controller("ngHomeController", function ($scope, $http, Albums, Songs) {
    $scope.showDetails = function (album) {
        for (var i in $scope.album) {
            delete $scope.album[i].showAlbumDetails;
        }
        if (album.showAlbumDetails){
            delete album.showAlbumDetails;
        } else {
            album.showAlbumDetails = true;
        };
        $scope.song = Songs;
        $http.get("api/song/" + album.Id).success(function (data) {
            $scope.song = data;
        }).error(function (data) {
            console.log(JSON.stringify(data));
        });
    };

    $scope.album = Albums;
    $http.get("api/album").success(function (data) {
        $scope.album = data;
    }).error(function (data) {
        console.log(JSON.stringify(data));
    });

    $scope.showSongDetails = function (song) {
        for (var i in $scope.song) {
            delete $scope.song[i].showSongDetail;
        }
        if (song.showSongDetail) {
            delete song.showSongDetail;
        } else {
            song.showSongDetail = true;
        };
    }
});

app.controller("ngAddAlbumController", function ($scope, $http, $location, Albums) {
    $scope.album = Albums;
    console.log("add album");
    $scope.addAlbum = function (album) {
        console.log(album);
        $http.post("/api/album", {title: album.Title, artist: album.Artist, image: album.Image, description: album.Description}).success(function (data) {
            album = data;
            $scope.title = '';
        }).error(function (data) {
            console.log(data);
            alert("Error adding!")
            $location.path("/");
        });
    };
});